var entity = require('../entity');
var Faction = entity.Faction;
var Goods = entity.Goods;


exports.newAndSave = function(params, callback){
    var goods = new Goods();
    goods.name = params.name;
    goods.description = params.description;
    goods.price = params.price;
    goods.count = params.count;
    goods.status = 'on';
    goods.belongFaction = params.factionId;
    goods.save(function(err, goods){
        callback(err, goods);
    });
}

exports.updateGoods = function(id, newGoods, callback){
    Goods.update({_id: id}, {$set: {
        name: newGoods.name,
        description: newGoods.description,
        price: newGoods.price,
        count: newGoods.count
    }}, {}, function(err, resp){
        callback(err, resp);
    })
}

exports.findById = function(goodsId,callback){
    Goods.findOne({_id: goodsId})
        .populate('belongFaction')
        .exec(function(err, goods){
        callback(err, goods);
    })
}

exports.findByFactionAndStatus = function(factionId, status, callback){
    var queryParams = {belongFaction: factionId}
    if(status)
        queryParams.status = status
    Goods.find(queryParams)
        .exec(function(err, goods){
            callback(err, goods);
        })
}

exports.onGoods = function(id, callback){
    Goods.update({_id: id}, {$set: {
        status: 'on'
    }}, {}, function(err, resp){
        callback(err, resp);
    })
}
exports.offGoods = function(id, callback){
    Goods.update({_id: id}, {$set: {
        status: 'off'
    }}, {}, function(err, resp){
        callback(err, resp);
    })
}
exports.delGoods = function(id, callback){
    Goods.remove({_id: id}, function(err, resp){
        callback(err, resp)
    })
}